package com.wh.weibo.controller;

import com.google.common.util.concurrent.RateLimiter;
import com.wh.weibo.facade.TopicService;
import com.wh.weibo.facade.WeiboService;
import com.wh.weibo.model.ServiceResult;
import com.wh.weibo.model.Topic;
import com.wh.weibo.model.User;
import com.wh.weibo.model.Weibo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author WeiHong
 * @date 2019/7/9 14:36
 */
@Controller
@RequestMapping("weibo")
public class WeiboController extends BaseController {

    @Resource
    private WeiboService weiboService;

    @Resource
    private TopicService topicService;

    /**
     *限制对应微博点赞的限流器
     */
    private RateLimiter rateLimiter = null;

    @PostConstruct
    public void init(){
        rateLimiter = RateLimiter.create(10);
    }

    @GetMapping("publish.html")
    public ModelAndView publishHtml() {
        User currentUser = super.getCurrentUser();
        if (null == currentUser) {
            ModelAndView modelAndView = new ModelAndView("redirect:/user/login.html");
            return modelAndView;
        }
        return new ModelAndView("publish");
    }

    @GetMapping("list")
    @ResponseBody
    public Map list() {
        ServiceResult<List<Weibo>> list;
        User currentUser = this.getCurrentUser();
        if (null == currentUser){
            list = weiboService.selectList();
        }else {
            list = weiboService.selectListByFeed(currentUser.getId());
        }
        if (null == list){
            return getErrorResp("调用微博信息服务异常");
        }else if (!list.isSuccess()){
            return getErrorResp(list.getErrorMsg());
        }else {
            return getResp(list.getData());
        }
    }


    /**
     * 根据微博ID查看微博详情
     *
     * @param id
     * @return
     */
    @GetMapping("get.html")
    public ModelAndView getHtml(@RequestParam(name = "id") Integer id) {
        ServiceResult<Weibo> result = weiboService.selectByPrimaryKey(id);
        if (null == result){
            ModelAndView modelAndView = new ModelAndView("error");
            modelAndView.addObject("content", "获取微博服务异常");
            return modelAndView;
        }else if (!result.isSuccess()){
            ModelAndView modelAndView = new ModelAndView("error");
            modelAndView.addObject("content", result.getErrorMsg());
            return modelAndView;
        }else {
            ModelAndView modelAndView = new ModelAndView("get");
            modelAndView.addObject("data", result.getData());
            return modelAndView;
        }
    }

    @PostMapping("publish")
    @ResponseBody
    public Map publish(@RequestParam(name = "content") String content,
                       @RequestParam(name = "imgUrl") String imgUrl,
                       @RequestParam(name = "topicTitle") String topicTitle) {
        User currentUser = super.getCurrentUser();
        if (null == currentUser) {
            return getErrorResp("当前用户未登录，无法发布微博");
        }

        //如果微博话题标题不为空，则通过话题标题查询出微博ID
        Integer topicId = 0;
        if (StringUtils.isNotBlank(topicTitle)){
            ServiceResult<Topic> topicResult = topicService.getByTitle(topicTitle);
            if (null == topicResult){
                return getErrorResp("获取微博话题服务异常");
            }else if (!topicResult.isSuccess()){
                return getErrorResp(topicResult.getErrorMsg());
            }else if (null == topicResult.getData()){
                return getErrorResp("话题不存在");
            }else {
                topicId = topicResult.getData().getId();
            }
        }

        Weibo createWeibo = new Weibo();
        createWeibo.setContent(content);
        createWeibo.setImgUrl(imgUrl);
        createWeibo.setUserId(currentUser.getId());
        createWeibo.setCreateTime(new Date());
        createWeibo.setTopicId(topicId);
        ServiceResult<Weibo> result = weiboService.create(createWeibo);
        if (null == result) {
            return getErrorResp("获取微博服务异常");
        } else if (!result.isSuccess()) {
            return getErrorResp(result.getErrorMsg());
        } else {
            return getResp(result.getData());
        }
    }
    @PostMapping("like")
    @ResponseBody
    public Map like(@RequestParam(name = "weiboId") Integer weiboId){
        User currentUser = super.getCurrentUser();
        if (null == currentUser) {
            return getErrorResp("当前用户未登录，无法点赞");
        }
        double limit = rateLimiter.acquire();
        if(limit > 0){
            return getErrorResp("活动太火爆，请稍后再试");
        }

        ServiceResult result = weiboService.like(currentUser.getId(), weiboId);
        if (null == result) {
            return getErrorResp("获取微博服务异常");
        } else if (!result.isSuccess()) {
            return getErrorResp(result.getErrorMsg());
        } else {
            return getResp(result.getData());
        }
    }

}
